home *** CD-ROM | disk | FTP | other *** search
/ Openstep 4.2 (Developer) / Openstep Developer 4.2.iso / NextDeveloper / Source / GNU / uucp / Uucp.framework / unix.subproj / move.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-09  |  4.2 KB  |  163 lines

  1. /* move.c
  2.    Move a file.
  3.  
  4.    Copyright (C) 1991, 1992, 1993 Ian Lance Taylor
  5.  
  6.    This file is part of the Taylor UUCP package.
  7.  
  8.    This program is free software; you can redistribute it and/or
  9.    modify it under the terms of the GNU General Public License as
  10.    published by the Free Software Foundation; either version 2 of the
  11.    License, or (at your option) any later version.
  12.  
  13.    This program is distributed in the hope that it will be useful, but
  14.    WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.    General Public License for more details.
  17.  
  18.    You should have received a copy of the GNU General Public License
  19.    along with this program; if not, write to the Free Software
  20.    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  21.  
  22.    The author of the program may be contacted at ian@airs.com or
  23.    c/o Cygnus Support, 48 Grove Street, Somerville, MA 02144.
  24.    */
  25.  
  26. #include "uucp.h"
  27.  
  28. #include "uudefs.h"
  29. #include "sysdep.h"
  30. #include "system.h"
  31.  
  32. #include <errno.h>
  33.  
  34. #if HAVE_FCNTL_H
  35. #include <fcntl.h>
  36. #else
  37. #if HAVE_SYS_FILE_H
  38. #include <sys/file.h>
  39. #endif
  40. #endif
  41.  
  42. /* Move (rename) a file from one name to another.  This routine will
  43.    optionally create necessary directories, and fpublic indicates
  44.    whether the new directories should be publically accessible or not.
  45.    If fcheck is true, it will try to determine whether the named user
  46.    has write access to the new file.  */
  47.  
  48. boolean
  49. fsysdep_move_file (zorig, zto, fmkdirs, fpublic, fcheck, zuser)
  50.      const char *zorig;
  51.      const char *zto;
  52.      boolean fmkdirs;
  53.      boolean fpublic;
  54.      boolean fcheck;
  55.      const char *zuser;
  56. {
  57.   struct stat s;
  58.   int o;
  59.  
  60.   DEBUG_MESSAGE2 (DEBUG_SPOOLDIR,
  61.           "fsysdep_move_file: Moving %s to %s", zorig, zto);
  62.  
  63.   /* Optionally make sure that zuser has write access on the
  64.      directory.  */
  65.   if (fcheck)
  66.     {
  67.       char *zcopy;
  68.       char *zslash;
  69.  
  70.       zcopy = zbufcpy (zto);
  71.       zslash = strrchr (zcopy, '/');
  72.       if (zslash == zcopy)
  73.     zslash[1] = '\0';
  74.       else
  75.     *zslash = '\0';
  76.  
  77.       if (stat (zcopy, &s) != 0)
  78.     {
  79.       ulog (LOG_ERROR, "stat (%s): %s", zcopy, strerror (errno));
  80.       ubuffree (zcopy);
  81.       return FALSE;
  82.     }
  83.       if (! fsuser_access (&s, W_OK, zuser))
  84.     {
  85.       ulog (LOG_ERROR, "%s: %s", zcopy, strerror (EACCES));
  86.       ubuffree (zcopy);
  87.       return FALSE;
  88.     }
  89.       ubuffree (zcopy);
  90.  
  91.       /* A malicious user now has a few milliseconds to change a
  92.      symbolic link to a directory uucp has write permission on but
  93.      the user does not (the obvious choice being /usr/lib/uucp).
  94.      The only certain method I can come up with to close this race
  95.      is to fork an suid process which takes on the users identity
  96.      and does the actual copy.  This is sufficiently high overhead
  97.      that I'm not going to do it.  */
  98.     }
  99.  
  100.   /* We try to use rename to move the file.  */
  101.  
  102.   if (rename (zorig, zto) == 0)
  103.     return TRUE;
  104.  
  105.   if (fmkdirs && errno == ENOENT)
  106.     {
  107.       if (! fsysdep_make_dirs (zto, fpublic))
  108.     return FALSE;
  109.       if (rename (zorig, zto) == 0)
  110.     return TRUE;
  111.     }
  112.  
  113. #if HAVE_RENAME
  114.   /* On some systems the system call rename seems to fail for
  115.      arbitrary reasons.  To get around this, we always try to copy the
  116.      file by hand if the rename failed.  */
  117.   errno = EXDEV;
  118. #endif
  119.  
  120.   /* If we can't link across devices, we must copy the file by hand.  */
  121.   if (errno != EXDEV)
  122.     {
  123.       ulog (LOG_ERROR, "rename (%s, %s): %s", zorig, zto,
  124.         strerror (errno));
  125.       return FALSE;
  126.     }
  127.  
  128.   /* Copy the file.  */
  129.   if (stat ((char *) zorig, &s) < 0)
  130.     {
  131.       ulog (LOG_ERROR, "stat (%s): %s", zorig, strerror (errno));
  132.       return FALSE;
  133.     }
  134.  
  135.   /* Make sure the file gets the right mode by creating it before we
  136.      call fcopy_file.  */
  137.   (void) remove (zto);
  138.   o = creat ((char *) zto, s.st_mode);
  139.   if (o < 0)
  140.     {
  141.       if (fmkdirs && errno == ENOENT)
  142.     {
  143.       if (! fsysdep_make_dirs (zto, fpublic))
  144.         return FALSE;
  145.       o = creat ((char *) zto, s.st_mode);
  146.     }
  147.       if (o < 0)
  148.     {
  149.       ulog (LOG_ERROR, "creat (%s): %s", zto, strerror (errno));
  150.       return FALSE;
  151.     }
  152.     }
  153.   (void) close (o);
  154.  
  155.   if (! fcopy_file (zorig, zto, fpublic, fmkdirs, FALSE))
  156.     return FALSE;
  157.  
  158.   if (remove (zorig) != 0)
  159.     ulog (LOG_ERROR, "remove (%s): %s", zorig, strerror (errno));
  160.  
  161.   return TRUE;
  162. }
  163.